﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace MLY.Bll
{

    public class clsSubContractor
    {

        #region Constructors
        // ==================================
        public clsSubContractor()
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = new Dal.TableOrganizationRelation();
            IsExist = false;
        }

        public clsSubContractor(int Id)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableOrganizationRelations
                        where (o.ContractorId == Id)
                        select o).First();

            IsExist = true;
        }
        // ==================================
        #endregion

        #region Static Members
        // ==================================
        public static List<Dal.TableOrganizationRelation> GetContractors()
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext(g.AppConnectionString))
            {
                return (from o in db.TableOrganizationRelations select o).ToList<Dal.TableOrganizationRelation>();
            }
        }
        // ==================================
        #endregion

        #region Public Methods
        // ==================================
        public void Save()
        {
            if (IsExist)
            {
                _db.SubmitChanges();
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdateBy = clsUser.CurrentUserId;
            }
            else
            {
                _DataRow.DateCreated = DateTime.Now;
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdateBy = clsUser.CurrentUserId;

                _db.TableOrganizationRelations.InsertOnSubmit(_DataRow);
                _db.SubmitChanges();
            }
        }
        // ==================================
        #endregion

        #region Public Properties
        // ==================================
        public Dal.TableOrganizationRelation Data
        {
            get { return _DataRow; }
        }
        // ==================================
        #endregion

        #region Fields
        // ==================================
        public bool IsExist = false;
        Dal.lqKbmDataContext _db = null;
        Dal.TableOrganizationRelation _DataRow = null;
        // ==================================
        #endregion
    }
}
